/**
 * 初始化部门详情对话框
 */
var TheTestInfoDlg = {
    TheTestInfoDlgData: {},
    zTreeInstance: null,
    validateFields: {}
};

/**
 * 清除数据
 */
TheTestInfoDlg.clearData = function () {
    this.TheTestInfoDlgData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TheTestInfoDlg.set = function (key, val) {
    this.TheTestInfoDlgData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
TheTestInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
TheTestInfoDlg.close = function () {
    parent.layer.close(window.parent.TheTestInfo.layerIndex);
}

/**
 * 收集数据
 */
TheTestInfoDlg.collectData = function () {
    this.set('id').set('theTestName').set('passWord').set('timeLength').set('typeOfQuestion').set('numberOfQuestion')
        .set('scoresOfQuestion').set('remark').set('scoresOfAdoption');
}

/**
 * 验证数据是否为空
 */
TheTestInfoDlg.validate = function () {
    $('#deptInfoForm').data("bootstrapValidator").resetForm();
    $('#deptInfoForm').bootstrapValidator('validate');
    return $("#deptInfoForm").data('bootstrapValidator').isValid();
}

TheTestInfoDlg.isPositiveInteger = function (s) {//是否为正整数
    var re = /^[0-9]+$/;
    return re.test(s)
}
/**
 * 提交添加考试
 */
TheTestInfoDlg.addSubmit = function () {
    var theTestName = $('#theTestName').val();
    var passWord = $('#passWord').val();
    var timeLength = $('#timeLength').val();
    var remark = $('#remark').val();
    var typeOfQuestion = $('#typeOfQuestion').val();
    var numberOfQuestion = $('#numberOfQuestion').val();
    var scoresOfQuestion = $('#scoresOfQuestion').val();
    var scoresOfAdoption = $('#scoresOfAdoption').val();
    if (theTestName == "" || theTestName == null) {
        alert("请输入考试名称！");
    }
    else if (passWord == "" || passWord == null) {
        alert("请输入考试密码！");
    }
    else if (numberOfQuestion == "" || numberOfQuestion == null) {
        alert("题目数量为正整数");
    }
    else if (scoresOfQuestion == "" || scoresOfQuestion == null) {
        alert("请输入每题分数！");
    }
    else if (passWord.length > 10) {
        alert("密码小于10！");
    }
    else if (!TheTestInfoDlg.isPositiveInteger(numberOfQuestion)) {
        alert("题目数量为整数");
    }
    else if (isNaN(scoresOfQuestion)) {
        alert("每题分数为数字");
    }
    else if (scoresOfAdoption!=null&&scoresOfAdoption!=""&&isNaN(scoresOfAdoption)) {
        alert("每题分数为数字");
    }
    else if (theTestName.length > 100) {
        alert("考试名称小于100！");
    }
    else if (remark.length > 100) {
        alert("备注长度小于100！");
    } else {
        this.clearData();
        this.collectData();
        //提交信息
        var ajax = new $ax(Feng.ctxPath + "/theTest/add", function (data) {
            if (data == 0) {
                Feng.error("考试名称已存在！请修改");
            } else {
                Feng.success("添加成功!");
                window.parent.TheTestInfo.table.refresh();
                TheTestInfoDlg.close();
            }
        }, function (data) {
            Feng.error("添加失败!" + data.responseJSON.message + "!");
        });
        ajax.set(this.TheTestInfoDlgData);
        ajax.start();
    }
}

//初始化数据
TheTestInfoDlg.initData = function () {
    //状态单选
    var seletimeLength = $('#itimeLength').val();
    if ($('#id').val() != "") {
        $("#timeLength option[value=" + seletimeLength + "]").attr("selected", "selected");
    }
}
/**
 * 提交修改
 */
TheTestInfoDlg.editSubmit = function () {
    var theTestName = $('#theTestName').val();
    var passWord = $('#passWord').val();
    var timeLength = $('#timeLength').val();
    var remark = $('#remark').val();
    var typeOfQuestion = $('#typeOfQuestion').val();
    var numberOfQuestion = $('#numberOfQuestion').val();
    var scoresOfQuestion = $('#scoresOfQuestion').val();
    var scoresOfAdoption = $('#scoresOfAdoption').val();
    if (theTestName == "" || theTestName == null) {
        alert("请输入考试名称！");
    }
    else if (passWord == "" || passWord == null) {
        alert("请输入考试密码！");
    }
    else if (numberOfQuestion == "" || numberOfQuestion == null) {
        alert("题目数量为正整数");
    }
    else if (scoresOfQuestion == "" || scoresOfQuestion == null) {
        alert("请输入每题分数！");
    }
    else if (scoresOfAdoption!=null&&scoresOfAdoption!=""&&isNaN(scoresOfAdoption)) {
        alert("每题分数为数字");
    }
    else if (passWord.length > 10) {
        alert("密码小于10！");
    }
    else if (theTestName.length > 100) {
        alert("考试名称小于100！");
    }
    else if (remark.length > 100) {
        alert("备注长度小于100！");
    }
    else if (!TheTestInfoDlg.isPositiveInteger(numberOfQuestion)) {
        alert("题目数量为正整数");
    }
    else if (isNaN(scoresOfQuestion)) {
        alert("每题分数为数字");
    }
    else if (theTestName.length > 100) {
        alert("考试名称小于100！");
    }
    else if (remark.length > 100) {
        alert("备注长度小于100！");
    } else {
        this.clearData();
        this.collectData();

        //提交信息
        var ajax = new $ax(Feng.ctxPath + "/theTest/update", function (data) {
            if (data == 0) {
                Feng.error("考试名称已存在！请修改");
            } else {
                Feng.success("修改成功!");
                window.parent.TheTestInfo.table.refresh();
                TheTestInfoDlg.close();
            }
        }, function (data) {
            Feng.error("修改失败!" + data.responseJSON.message + "!");
        });
        ajax.set(this.TheTestInfoDlgData);
        ajax.start();
    }
}

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentDeptMenu" || $(
            event.target).parents("#parentDeptMenu").length > 0)) {
        TheTestInfoDlg.hideDeptSelectTree();
    }
}

$(function () {
    Feng.initValidator("deptInfoForm", TheTestInfoDlg.validateFields);
    TheTestInfoDlg.initData();
    var ztree = new $ZTree("parentDeptMenuTree", "/dept/tree");
    ztree.bindOnClick(TheTestInfoDlg.onClickDept);
    ztree.init();
    TheTestInfoDlg.zTreeInstance = ztree;
});
